import $ from 'jquery'

export default {
    state: {
        id: "",
        username: "",
        phone: "",
        email: "",
        token: "",
        roleId: "",  
        is_login: false,
        pulling_info: true,  //是否正在从云端拉去信息(解决加载后页面突然跳转的问题)
    },
    getters: {
    },
    mutations: {
        updateUser(state, user) {   //更新用户信息
            state.id = user.id;
            state.username = user.username;
            state.phone = user.phone;
            state.email = user.email;
            state.roleId = user.roleId;
            state.is_login = user.is_login;
        },
        updateToken(state, token) {   //更新用户token
            state.token = token;
        },
        logout(state) {               //退出登录
            state.id = "";
            state.username = "";
            state.phone = "";
            state.email = "";
            state.token = "";
            state.roleId = "",
            state.is_login = false;

        },
        updatePullingInfo(state, pulling_info){
            state.pulling_info = pulling_info;
        }
    },
    actions: {
        login(context, data){  //登录
            $.ajax({
                url: 'http://localhost:8090/sls/account/token',
                type: "post",
                data: {
                    username : data.username,
                    password : data.password,
                    roleId : data.roleId,
                },
                success(resp){
                    // console.log("resp-in-user.js:");
                    // console.log(resp);
                    if(resp.error_message === "success"){
                        localStorage.setItem("jwt_token", resp.token);
                        localStorage.setItem("rid", resp.roleId);
                        context.commit("updateToken", resp.token);  //如果用户名和密码正确, 则更新token
                        data.success(resp);
                    }else{
                        data.error(resp);
                    }
                },
                error(resp){
                     //console.log(resp);
                     data.error(resp);
                }
            })
        },

        getInfo(context, data){  //获取用户信息
            $.ajax({   
                url: 'http://localhost:8090/sls/account/info',
                type: 'get',
                headers: {
                    Authorization: "Bearer " + context.state.token,
                },
                success(resp){
                    //console.log(resp);
                    if(resp.error_message === 'success'){
                        //console.log("context.state.token");
                        //console.log(context.state.token);
                        //localStorage.setItem("jwt_token", resp.token);  //登录时,将token存到localStorage中, 实现登录状态持久化
                        context.commit("updateUser", {
                            ...resp,
                            is_login : true,
                        });
                        data.success(resp);
                    }
                    else{
                        data.error(resp);
                    }
                },
                error(resp){
                    //console.log(resp);
                    data.error(resp);
                }
        })
    },
        logout(context){   //退出登录
            localStorage.removeItem("jwt_token");  //退出登录时,将token从localStore中删除
            localStorage.removeItem("rid");
            context.commit("logout");
        }
    },

    modules: {
    }
}
